package com.dns.customers;

import com.customer.dns.client.CuCustomerService;
import com.customer.dns.client.CuCustomerServicePortType;
import com.util.JsonToXMLUtils;
import com.util.XmlToJsonUtils;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;

/**
 * DNS 服务基类
 * 1、通用日志调用
 * 2、润通webService 接口调用
 *
 * @author :  yulei
 * @data :  2018/5/28 18:50
 * @Version :  1.0
 **/

    @Component
    public class AppPage {

    public AppPage()  {
        
    }
    @Value("#{prop.access_ip}")
    private  String access_ip;

    @Value("#{prop.domainTemplateName}")
    private  String domainTemplateName;

    public String getDomainTemplateName() {
        return domainTemplateName;
    }

    public String getAccess_ip() {

        return access_ip;
    }

    @Autowired
    CuCustomerService cuCustomerService;

    //新增大客户
    public  final   String  ADD_CUSTOMER_ACCOUNT="addCustomerAccount";
    //删除大客户
    public  final  String  DEL_CUSTOMER_ACCOUNT="delCustomerAccount";
    //查询大客户
    public final  String QUERY_CUSTOMER_ACCOUNT="queryCustomerAccount";



    //新增和修改大客户根域名记录信息
    public final  String MODIFY_CUSTOMER_ROOT_RECORD="modifyCustomerRootRecord";
    //查询已配置的根域名信息
    public final  String QUERY_CUSTOMER_ROOT_CONFIG="queryCustomerRootConifg";
    public final  String DEL_CUSTOMER_ROOT_RECORD="delCustomerRootRecord";


    //大客户用户增加和修改需要监控的子域名信息
    public final  String ADD_CUSTOMER_CONFIG="addCustomerConfig";
    //大客户删除子域名信息或子域名记录信息
    public final  String DEL_CUSTOMER_CONFIG="delCustomerConfig";

    //下发清除域名缓存任务
    public final String ADD_CLEAN_DOMAIN_CACHE_TASK="addCleanDomainCacheTask";
    //查询上一次下发的清除域名缓存任务的结果信息
    public final String QUERY_CLEAN_DOMAIN_CACHE="queryCleanDomainCache";

    //DNS 解析
    //下发向全国DNS查询解析结果的任务
    public final String ADD_DNS_RESULT_TASK="addDnsResultTask";
    //查询上一次下发的域名解析查询任务的结果信息
    public final String QUERY_DNS_RESULT="queryDnsResult";

    //查询最近一次周期性对大客户配置域名的解析结果
    public final String QUERY_DNS_RESULT_TASK="queryDnsResultTask";

    protected   Logger log = LoggerFactory.getLogger(getClass());

    //润通接口调用
    protected JSONObject invokeRTWebService(JSONObject custJson,String custMethod) throws Exception {

        String customerXml = new JsonToXMLUtils().write(custJson);
        log.debug("customerXml = " + customerXml);
        CuCustomerServicePortType customerService = cuCustomerService.getCuCustomerServiceHttpsSoap11Endpoint();

        String responseXml=null;
        switch (custMethod){

            case ADD_CUSTOMER_ACCOUNT:
                // 新增/修改大客户信息
               responseXml = customerService.addCustomerAccount(customerXml);
               break;
            case DEL_CUSTOMER_ACCOUNT:
                //删除大客户信息
                responseXml = customerService.delCustomerAccount(customerXml);
                break;
            case  QUERY_CUSTOMER_ACCOUNT:
                //查询大客户信息
                responseXml = customerService.queryCustomerAccount(customerXml);
                break;
            case  MODIFY_CUSTOMER_ROOT_RECORD:
                //新增和修改大客户根域名记录信息
                responseXml = customerService.modifyCustomerRootRecord(customerXml);
                break;
            case  QUERY_CUSTOMER_ROOT_CONFIG:
                //新增和修改大客户根域名记录信息
                responseXml = customerService.queryCustomerRootConifg(customerXml);
                break;
            case  DEL_CUSTOMER_ROOT_RECORD:
                //删除大客户根域名类型
                responseXml = customerService.deleteCustomerRootRecord(customerXml);
                break;
            case  ADD_CUSTOMER_CONFIG:
                //大客户用户增加和修改需要监控的域名信息
                responseXml = customerService.addCustomerConfig(customerXml);
                break;

            case  DEL_CUSTOMER_CONFIG:
                //删除子域名及其解析记录信息
                responseXml = customerService.delCustomerConfig(customerXml);
                break;
            case  ADD_CLEAN_DOMAIN_CACHE_TASK:
                //下发清除域名缓存任务
                responseXml = customerService.addCleanDomainCacheTask(customerXml);
                break;

            case  QUERY_CLEAN_DOMAIN_CACHE:
                //查询上一次下发的清除域名缓存任务的结果信息
                responseXml = customerService.queryCleanDomainCache(customerXml);
                break;

            case  ADD_DNS_RESULT_TASK:
                //查询上一次下发的清除域名缓存任务的结果信息
                responseXml = customerService.addDnsResultTask(customerXml);
                break;
            case  QUERY_DNS_RESULT:
                //查询上一次下发的域名解析查询任务的结果信息
                responseXml = customerService.queryDnsResult(customerXml);
                break;
            case  QUERY_DNS_RESULT_TASK:
                //查询最近一次周期性对大客户配置域名的解析结果
                responseXml = customerService.queryDnsResultTask(customerXml);
                break;

            default:
                JSONObject json = new JSONObject();
                json.put("returncode", com.dns.customers.ErrorMssage.NOT_THIS_METHOD);
                json.put("errorMsg", com.dns.customers.ErrorMssage.NOT_THIS_METHOD);
        }

        log.debug("responseXml = " + responseXml);

        // XML转JSON
        String resultJsonStr = XmlToJsonUtils.documentToJSONObject(responseXml);
        log.debug("resultJsonStr = " + resultJsonStr);
        //********取出润通返回的信息********
        JSONObject responseJson = JSONObject.fromObject(resultJsonStr);

       return  responseJson;
    }


    /**
     * 获取客户端的IP地址
     * @param request
     * @return
     */
    public String getRemoteAddr(HttpServletRequest request){

        String ipAddress = request.getHeader("x-forwarded-for");
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
                //根据网卡取本机配置的IP
                InetAddress inet=null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ipAddress= inet.getHostAddress();
            }
        }
        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
            if(ipAddress.indexOf(",")>0){
                ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }


    /**
     * 获取本地主机的IP地址
     * @return
     */
    public String  getLocalIP() throws Exception {
        //实例化InetAddress对象
        InetAddress   ip=InetAddress.getLocalHost();
        //获取本地ip地址
        String  localIp=ip.getHostAddress();
        return  localIp;
    }

}
